{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Matplotlib\n",
    "## Introduction\n",
    "- matplotlib is probably the single most used Python package for 2D-graphics\n",
    "- it also provides good capablities to creade 3D-graphics\n",
    "- quick way to visualize data from python in publication-quality\n",
    "\n",
    "- for further information: https://matplotlib.org/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Creating First Plots\n",
    "\n",
    "### 1. Import pyplot package\n",
    "    - provides functions that makes matplotlib work like MATLAB\n",
    "    - object-oriented plotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt # import pyplot interface\n",
    "plt.style.use('default')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 3. Create [figure](https://matplotlib.org/api/figure_api.html) and [axes](https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "fig = plt.figure() # a new figure window\n",
    "ax = fig.add_subplot(1, 1, 1) # a new axes\n",
    "plt.show(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 3. Create / [Plot data](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html) (sine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "x = np.linspace(0,10,1000)\n",
    "y = np.sin(x)\n",
    "\n",
    "ax.plot(x,y, label = 'sine')\n",
    "fig # this is required to re-display the figure"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Customize Line Style"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "fig2 = plt.figure() # a new figure window\n",
    "ax2 = fig2.add_subplot(1, 1, 1) # a new axes\n",
    "\n",
    "x2 = np.linspace(0,10,50)\n",
    "y2 = np.sin(x2)\n",
    "\n",
    "ax2.plot(x2,y2, '-o', label = 'sine')\n",
    "plt.show(fig2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "fig3 = plt.figure() # a new figure window\n",
    "ax3 = fig3.add_subplot(1, 1, 1) # a new axes\n",
    "\n",
    "x2 = np.linspace(0,10,50)\n",
    "y2 = np.sin(x2)\n",
    "\n",
    "ax3.plot(x2,y2, 'r-o', label = 'sine')\n",
    "plt.show(fig3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "##### Line Colour\n",
    "'r': red  \n",
    "'g': green  \n",
    "'b': blue  \n",
    "'c': cyan  \n",
    "'m': magenta  \n",
    "'y': yellow  \n",
    "'k': black  \n",
    "'w:': white  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "##### Line Style\n",
    "'-': solid  \n",
    "'--': dashed  \n",
    "':': dotted  \n",
    "'-.': dot-dashed  \n",
    "'.': points  \n",
    "'o': filled circles  \n",
    "'^': filled triangles  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 4. Create / [Plot data](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html) (cosine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "y2 = np.cos(x)\n",
    "ax.plot(x,y2, label = 'cosine')\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 5. Create / [Plot data](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html) (3 * cosine) on second axes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax_twin = ax.twinx()\n",
    "y3 = 3 * np.cos(x+np.pi/4)\n",
    "ax_twin.plot(x,y3, 'r',label = '3 * cosine')\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 5. Set limits for [x](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xlim.html)-/[y](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_ylim.html)-axis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "ax.set_xlim(0,10)\n",
    "ax.set_ylim(-1.5, 2.0)\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 6. [Add legend](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "ax.legend()\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 7. Add [x](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xlabel.html)-/[y](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_ylabel.html)-label and [title](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_title.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "ax.set_xlabel(r\"$x$\")\n",
    "ax.set_ylabel(r\"$\\sin(x)$\")\n",
    "ax.set_title(r\"I like $\\pi$\")\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 7. [Add grid](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.grid.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax.grid(True)\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Excursion Subplots\n",
    "- the command [fig.add_subplot](https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html) divides the figures in grid with a certain number of axes\n",
    "- syntax:\n",
    "``` python    \n",
    "fig.add_subplot(rows, cols, num)\n",
    "```    \n",
    "- rows = number of rows in the grid\n",
    "- cols = number of columns in the grid\n",
    "- num = number of the subplot to create (counting from left to right, top to bottom and indexed starting at 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "fig  =  plt.figure()\n",
    "for  i  in range(6):\n",
    "    ax  =  fig.add_subplot(2, 3, i + 1)\n",
    "    ax.set_title(\"Plot #%i\" % i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- the subplots are overlapping\n",
    "- there are a few ways to fix it, i.e.:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "fig.subplots_adjust(wspace=0.4, hspace=0.4)\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- ```wspace``` and ```hspace ``` determine the width and height between each plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 2. Various 2D Plotting\n",
    "### [Histograms](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "x = np.random.normal(size=1000)\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "H = ax.hist(x, bins=50, alpha=0.5, histtype='stepfilled')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### [Pie Plot](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "fracs = [30, 15, 45, 10]\n",
    "colors = ['b', 'g', 'r', 'w']\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(6, 6))  # make the plot square\n",
    "pie = ax.pie(fracs, colors=colors, explode=(0, 0, 0.05, 0), shadow=True,\n",
    "             labels=['A', 'B', 'C', 'D'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### [Errorbar Plots](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.errorbar.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "x = np.linspace(0, 10, 30)\n",
    "dy = 0.1\n",
    "y = np.random.normal(np.sin(x),dy)\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "plt.errorbar(x, y, dy, fmt='.k')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### [Contour Plots (filled)](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.contourf.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "x = np.linspace(0, 10, 50)\n",
    "y = np.linspace(0, 20, 60)\n",
    "\n",
    "z = np.cos(y[:, np.newaxis]) * np.sin(x)\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "# filled contours\n",
    "im = ax.contourf(x, y, z, 100)\n",
    "\n",
    "fig.colorbar(im, ax=ax)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### [Contour Plots (lines)](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.contour.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "# contour lines\n",
    "im2 = ax.contour(x, y, z, colors='k')\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 3. [Various 3D Plotting](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "# This is the 3D plotting toolkit\n",
    "from mpl_toolkits.mplot3d import Axes3D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### [3D scatter Plot](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#scatter-plots)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "ax = plt.axes(projection='3d')\n",
    "\n",
    "z = np.linspace(0, 1, 100)\n",
    "x = z * np.sin(20 * z)\n",
    "y = z * np.cos(20 * z)\n",
    "\n",
    "c = x + y\n",
    "\n",
    "ax.scatter(x, y, z, c=c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### [3D Line Plot](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#line-plots)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "ax = plt.axes(projection='3d')\n",
    "\n",
    "ax.plot(x, y, z, '-b')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### [Surface Plot](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.outer(np.linspace(-2, 2, 30), np.ones(30))\n",
    "y = x.copy().T\n",
    "z = np.cos(x ** 2 + y ** 2)\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = plt.axes(projection='3d')\n",
    "\n",
    "ax.plot_surface(x, y, z, cmap=plt.cm.jet, rstride=1, cstride=1, linewidth=0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "RL25",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
